package com.chwn.curiousdiary.ui.user.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.adapter.CollectionAdapter;
import com.chwn.curiousdiary.adapter.StoryListAdapter;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.bean.StoryListBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.service.AudioService;
import com.chwn.curiousdiary.ui.play.activity.StoryPlayActivity;
import com.chwn.curiousdiary.ui.user.presenter.CollectionPresenter;
import com.chwn.curiousdiary.ui.user.contract.CollectionContract;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.interfaces.OnItemClickListener;
import com.chwn.harulib.mvp.base.BaseMVPFragment;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Myshikin on 2017/4/25.
 */

public class CollectionFragment extends BaseMVPFragment<CollectionPresenter> implements CollectionContract.View,
        XRecyclerView.LoadingListener {


    private XRecyclerView     mCollectionRV;
    private CollectionAdapter mCollectionAdapter;

    private StoryListAdapter mStoryListAdapter;

    /**
     *
     */
    private int     page      = 1;
    private boolean isRefresh = true;
    private boolean isHistoryPage;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_only_recyclerview;
    }

    @Override
    protected void initData() {
        super.initData();
        isHistoryPage = getArguments().getBoolean(Constant.IS_HISTORY_PAGE, false);
    }

    @Override
    protected void initContentView() {
        mCollectionRV = (XRecyclerView) mInflateContentView.findViewById(R.id.xrv_message);
        mInflateContentView.setBackgroundColor(mActivity.getResources().getColor(R.color.app_bg_white));
        mCollectionRV.setLoadingMoreEnabled(true);
        mCollectionRV.setLoadingListener(this);
        mCollectionRV.setLayoutManager(new LinearLayoutManager(mActivity));


        if (isHistoryPage) {
            mStoryListAdapter = new StoryListAdapter(mActivity, null, true);
            mCollectionRV.setAdapter(mStoryListAdapter);
            mStoryListAdapter.setOnItemClickListener(new OnItemClickListener<StoryListBean>() {
                @Override
                public void onItemClick(ViewHolder viewHolder, StoryListBean data, int position) {
                    onStoryListItemClick(data, position);
                }
            });
        } else {
            mCollectionAdapter = new CollectionAdapter(mActivity, null, false);
            mCollectionRV.setAdapter(mCollectionAdapter);
            mCollectionAdapter.setOnItemClickListener(new OnItemClickListener<StoryBean>() {
                @Override
                public void onItemClick(ViewHolder viewHolder, StoryBean data, int position) {
                    ArrayList<StoryBean> list = (ArrayList<StoryBean>) mCollectionAdapter.getDatas();

                    Intent intent = new Intent(getActivity(), StoryPlayActivity.class);
                    intent.putExtra(Constant.STORY_SERVICE_TYPE, AudioService.HOME_ENTER);
                    intent.putExtra(Constant.PACKAGE_ID, Constant.PACKAGE_ID);
                    intent.putExtra("list", list);
                    intent.putExtra("position", position);
                    startActivity(intent);
                }
            });
        }


        initStateViewData();
    }

    private void onStoryListItemClick(StoryListBean data, int position) {
        List<StoryListBean> datas;

        datas = mStoryListAdapter.getDatas();

        ArrayList<StoryBean> storyList = new ArrayList<>();

        for (int i = 0; i < datas.size(); i++) {
            storyList.add(new StoryBean(datas.get(i)));
        }

        Random random = new Random();
        Intent intent = new Intent(getActivity(), StoryPlayActivity.class);
        intent.putExtra(Constant.STORY_SERVICE_TYPE, AudioService.NEWEST_ENTER);
        intent.putExtra(Constant.PACKAGE_ID, random.nextInt() + "history" + random.nextInt());
        intent.putExtra("list", storyList);
        intent.putExtra("position", position);
        startActivity(intent);
    }

    private void initStateViewData() {
        setEmptyView(isHistoryPage ? R.drawable.icon_no_history : R.drawable.icon_no_collecte);
        setErrorView(isHistoryPage ? R.drawable.icon_no_history : R.drawable.icon_no_collecte);
        setErrorText("哎呀，快去检查一下网络吧～");
        setEmptyText(isHistoryPage ? "您还没有给宝宝听故事哦，快去听听吧~" : "您还没有收藏故事呢，赶紧去听吧～");
    }

    @Override
    protected void getData() {
        mPresenter.getCollectionStories(page, isHistoryPage);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getCollectionStories(page, isHistoryPage);
    }

    @Override
    public void onRequestError(Throwable e) {
        super.onRequestError(e);
        if (!isRefresh && e instanceof HttpResultException) {
            mCollectionRV.loadMoreComplete();
            mCollectionRV.refreshComplete();
            mCollectionRV.noMoreLoading();
            page--;
        } else if (e instanceof HttpResultException) {
            mCollectionRV.noMoreLoading();
            if (isHistoryPage) {
                if (mStoryListAdapter.getDatas() == null || mStoryListAdapter.getDatas().size() < 1) {
                    showEmptyView();
                } else {
                    showEmptyView();
                }
            } else {
                if (mCollectionAdapter.getDatas() == null || mCollectionAdapter.getDatas().size() < 1) {
                    showEmptyView();
                } else {
                    showEmptyView();
                }
            }

        } else {
            if (isHistoryPage) {
                if (mStoryListAdapter.getDatas() == null || mStoryListAdapter.getDatas().size() < 1) {
                    showErrorView();
                }
            } else {
                if (mCollectionAdapter.getDatas() == null || mCollectionAdapter.getDatas().size() < 1) {
                    showErrorView();
                }
            }
        }
    }


    @Override
    public void onRefresh() {
        page = 1;
        isRefresh = true;
        mPresenter.getCollectionStories(page, isHistoryPage);
    }

    @Override
    public void onLoadMore() {
        page++;
        isRefresh = false;
        mPresenter.getCollectionStories(page, isHistoryPage);
    }

    @Override
    public void showCollectionStories(List<StoryBean> stories) {
        showContentView();
        if (stories.size() < Constant.PAGE_COUNT) {
            mCollectionRV.noMoreLoading();
        }
        if (isRefresh) {

            if (isHistoryPage) {
                ArrayList<StoryListBean> storyLists = new ArrayList<>();
                for (int i = 0; i < stories.size(); i++) {
                    storyLists.add(new StoryListBean(stories.get(i)));
                }
                mStoryListAdapter.setNewData(storyLists);
            } else {
                mCollectionAdapter.setNewData(stories);
            }
            mCollectionRV.refreshComplete();
        } else {

            if (isHistoryPage) {
                ArrayList<StoryListBean> storyLists = new ArrayList<>();
                for (int i = 0; i < stories.size(); i++) {
                    storyLists.add(new StoryListBean(stories.get(i)));
                }
                mStoryListAdapter.setLoadMoreData(storyLists);
            } else {
                mCollectionAdapter.setLoadMoreData(stories);
            }
            mCollectionRV.loadMoreComplete();
        }
    }


}
